package com.bm.specialvehiclefordispatcher.activity.order;

import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.mywallet.RechargeMoneyActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.bean.Order;
import com.bm.specialvehiclefordispatcher.bean.OrderLog;
import com.bm.specialvehiclefordispatcher.bean.ServiceLog;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.logics.ServiceLogManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;
import com.bm.specialvehiclefordispatcher.views.CustomDialog;
import com.bm.specialvehiclefordispatcher.views.RaitingBarForShowing;
import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.squareup.picasso.Picasso;

public class CheckOutActivity extends BaseActivity implements OnClickListener {

	public static final String ORDER = "ORDER";
	public static final String SERVISE_LOGS = "SERVISE_LOGS";
	public static final String OWNER = "OWNER";

	private CommonNavigationBar navi;
	private TextView tv_name, tv_licenseplateNum, tv_style, tv_owner_status, tv_price, tv_volume;
	private ImageView iv_icon, iv_tel;
	private RaitingBarForShowing ratingbar;
	private Button btn_checkout;

	private Order order;
	private OrderLog ol;
	private List<ServiceLog> serviceLogs;
	private CustomDialog dialog;
	private ServiceLogManager manager = new ServiceLogManager();
	private int sumVolume = 0;
	private double sumPrice = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_checkout);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		navi = (CommonNavigationBar) findViewById(R.id.navi);
		tv_name = (TextView) findViewById(R.id.tv_name);
		ratingbar = (RaitingBarForShowing) findViewById(R.id.ratingbar);
		tv_licenseplateNum = (TextView) findViewById(R.id.tv_licenseplateNum);
		tv_style = (TextView) findViewById(R.id.tv_style);
		tv_owner_status = (TextView) findViewById(R.id.tv_owner_status);
		iv_icon = (ImageView) findViewById(R.id.iv_icon);
		iv_tel = (ImageView) findViewById(R.id.iv_tel);
		tv_price = (TextView) findViewById(R.id.tv_price);
		tv_volume = (TextView) findViewById(R.id.tv_volume);
		btn_checkout = (Button) findViewById(R.id.btn_checkout);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void init() {
		navi.setTitle(R.string.owner_detail);
		navi.setListener(null);
		order = (Order) getIntent().getSerializableExtra(ORDER);
		ol = (OrderLog) getIntent().getSerializableExtra(OWNER);
		serviceLogs = (List<ServiceLog>) getIntent().getSerializableExtra(SERVISE_LOGS);
		dialog = new CustomDialog(mActivity);
		countSum();
		setOwnerData();
	}

	private void countSum() {
		sumVolume = 0;
		sumPrice = 0;
		for (int i = 0; i < serviceLogs.size(); i++) {
			ServiceLog log = serviceLogs.get(i);
			sumVolume += log.volume;
			sumPrice += log.realsumPrice;
		}
		tv_price.setText("¥" + sumPrice);
		tv_volume.setText(sumVolume + "");
	}

	private NListener<BaseData> getListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					ToastMgr.show(R.string.checkout_msg1);
					setResult(RESULT_OK);      
					finish();
				} else if (data.status == 8) {
					ToastMgr.show("账户余额不足，请充值");
					startActivity(new Intent(mActivity, RechargeMoneyActivity.class));
				}
				else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
			}
		};
	}

	private void setOwnerData() {
		tv_name.setText(ol.driver);
		tv_licenseplateNum.setText(ol.licensePlate + ol.licensePlateNum);
		ratingbar.setRate(ol.averageScore);
		tv_style.setText(ol.style1 + ":" + ol.styleName);
		Picasso.with(mActivity).load(ol.icon).resize(480, 480).centerCrop().transform(new RoundedTransformationBuilder().oval(true).build()).into(iv_icon);

		long id = ol.statusId;

		if (order.status == 2 || order.status == 3) {
			tv_owner_status.setVisibility(View.VISIBLE);
			tv_owner_status.setText(ol.getStatusMsg(id));
		} else {
			tv_owner_status.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void addListeners() {
		iv_tel.setOnClickListener(this);
		btn_checkout.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_checkout:
			dialog.show();
			dialog.justShowMsg("结算总金额为¥" + sumPrice + "元，确定结算？", new OnClickListener() {
				@Override public void onClick(View v) {
					dialog.dismiss();
					String ids = "";
					for (int i = 0; i < serviceLogs.size(); i++) {
						ids += (serviceLogs.get(i).id + ",");
					}
					
					ids = ids.substring(0, ids.length() - 1);
					loadingDialog.show();
					manager.checkOut(ids, LoginManager.getInstance(mActivity).getUid(), ol.ownerId, ol.truckId, getListener());
				}
			});
			break;

		case R.id.iv_tel:
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ol.owner.phone));
			startActivity(callIntent);
			break;
		}
	}

}
